package androidcamp.socialfitnesstracker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidcamp.socialfitnesstracker.data.Workout;
import androidcamp.socialfitnesstracker.data.WorkoutType;

import java.util.Date;

public class Home extends Activity implements OnClickListener {
	
    Button goals, new_workout, history;
    
    public static final int NEW_WORKOUT = 1; 
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        goals = (Button)findViewById(R.id.goals_button);
        goals.setOnClickListener(this);
        
        new_workout = (Button)findViewById(R.id.new_workout_button);
        new_workout.setOnClickListener(this);
        
        history = (Button)findViewById(R.id.history_button);
        history.setOnClickListener(this);
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    }

	@Override
	public void onClick(View v) {
		Intent i;
		switch (v.getId()) {
		case R.id.goals_button:
            i = new Intent(this, Goals.class);
            startActivity(i);
			break;
		case R.id.new_workout_button:
			i = new Intent(this, NewWorkout.class);
			startActivityForResult(i, NEW_WORKOUT);
			break;
		case R.id.history_button:
			i = new Intent(this, NewWorkout.class);
			startActivity(i);
			break;
		default:
				finish();
		}
		
	}
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case NEW_WORKOUT:
                if(resultCode == RESULT_OK) {
                    long timeSpent = data.getExtras().getLong(DoingActivity.ACTIVITY_TIME_SPENT);
                    WorkoutType workoutType = (WorkoutType) data.getExtras().get(DoingActivity.WORKOUT_ACTIVITY_TYPE);
                    Log.d(WorkoutActivityTimer.LOG_TAG,
                            String.format("SAVE: %s for %d seconds", workoutType, timeSpent));
                    Workout newWorkout = new Workout(workoutType, timeSpent,
                            new Date(new Date().getTime() - timeSpent));
                    MockDB.get().insertNewWorkout(newWorkout);
                }
                break;
            default:
                Log.e(WorkoutActivityTimer.LOG_TAG, "unknown request code!");
        }
    }
    
    
}
